<template>
  <q-page class="q-pa-md">
    <div class="row items-center">
      <div class="q-pb-md text-h5">
        {{ pageTitle }}
        <span class="text-subtitle1">列表</span>
      </div>
      <q-space></q-space>
      <search-bar-toggle-btn v-model="searchBarVisibility"></search-bar-toggle-btn>
    </div>
    <div class="q-pa-sm shadow-2 q-mb-md items-center" v-show="searchBarVisibility">
      <div class="row q-col-gutter-sm">
        <div class="col-12 col-sm-6 col-md-4 col-lg-3 row q-col-gutter-sm">
          <div class="col-4 text-right searchbar-label">OBDNumber</div>
          <div class="col-8">
            <easy-input v-model="fpsReq.obdNumber" @focus="fx.ui.select" @keyup.enter="loadListData(2)"
              :readonly="listDataLoadingState">
            </easy-input>
          </div>
        </div>
        <div class="col-12 col-sm-6 col-md-4 col-lg-3 row q-col-gutter-sm">
          <div class="col-4 text-right searchbar-label">创建日期从</div>
          <div class="col-8">
            <easy-input v-model="fpsReq.createDtBeg" use-date-picker append-icon-show
              :readonly="listDataLoadingState" />
          </div>
        </div>
        <div class="col-12 col-sm-6 col-md-4 col-lg-3 row q-col-gutter-sm">
          <div class="col-4 text-right searchbar-label">创建日期至</div>
          <div class="col-8">
            <easy-input v-model="fpsReq.createDtEnd" use-date-picker append-icon-show
              :readonly="listDataLoadingState" />
          </div>
        </div>
        <div class="col-shrink row q-col-gutter-sm">
          <div class="col">
            <q-toggle v-model="fpsReq.isSuccess" :disable="listDataLoadingState" @update:model-value="loadListData(2)"
              dense toggle-indeterminate class="q-ma-xs" label="是否成功" />
          </div>
        </div>
      </div>
      <div class="row q-pt-sm">
        <div class="q-mr-sm">
          <q-btn :disable="listDataLoadingState" @click="loadListData(2)" color="primary" icon="search" label="查询" />
        </div>
      </div>
    </div>
    <div class="shadow-2">
      <q-table @request="fpsAssist.onDataTableRequest" :rows="$d.tableData" :columns="fpsInfo.columnsInfo"
        separator="cell" row-key="pgiId" v-model:pagination="fpsInfo.paginationInfo"
        :visible-columns="fpsInfo.visibleColumns"
        dense flat :hide-pagination="true" :loading="listDataLoadingState" color="primary" binary-state-sort
        hide-selected-banner v-model:selected="$d.selectedItems" selection="multiple"
        @row-dblclick="(evt,row)=>{fpsNav.gotoDetail(row.pgiId)}">
        <template v-slot:top="props">
          <!--填充所有剩余空间-->
          <div class="q-gutter-sm">
            <q-btn color="primary" label="发送" @click="onSend" :disable="$d.selectedItems.length===0"/>
          </div>
          <q-space />
          <table-col-options-cfg v-model:table-columns="fpsInfo.columnsInfo"
            v-model:visible-columns="fpsInfo.visibleColumns"
            @update-col-options-for-save="fpsAssist.saveColOptions">
          </table-col-options-cfg>
          <q-btn flat dense :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
            :title="props.inFullscreen ? '退出全屏' : '全屏显示'" @click="props.toggleFullscreen" class="q-ml-sm"
            :ripple="false" />
        </template>
        <!--操作列插槽-->
        <template v-slot:body-cell-operation="props">
          <q-td :props="props" auto-width>
            <q-btn color="primary" label="查看" class="q-mr-xs btn-in-row" :dense="true"
              @click="evt => fpsNav.gotoDetail(props.row.pgiId, evt)" />
          </q-td>
        </template>
        <template v-slot:body-cell-isSuccess="props">
          <q-td :props="props" auto-width>
            <q-badge :color="props.row.isSuccess?'positive':'grey-6'"
              :label="props.row.isSuccess ? '是' : '否'" />
          </q-td>
        </template>
        <template v-slot:no-data>
          <div class="full-width items-center justify-center row">
            <q-icon name="error_outline" />
            无数据
            <q-btn flat icon="refresh" :loading="listDataLoadingState"
              :disable="listDataLoadingState" dense title="刷新" size="sm" class="q-ml-sm"
              @click="loadListData"></q-btn>
          </div>
        </template>
        <template v-slot:bottom>
          <paging-bar :pageNo="fpsReq.pageNo" :pageSize="fpsReq.pageSize" :totalAmount="fpsInfo.totalAmount"
            @pagesize-changed="fpsAssist.onPageSizeChanged" @go-to-page="fpsNav.gotoPage"
            :loading-state="listDataLoadingState" />
        </template>
      </q-table>
    </div>
  </q-page>
</template>

<script lang="ts">
export default {
  name: 'IbdList'
};
</script>

<script lang="ts" setup>
import { useTitleHelper } from '@njt/foundation/hooks/titleHelper';
import { useFpsHelper } from '@njt/foundation/hooks/fpsHelper';
import { reactive, ref } from 'vue';
import { ColumnInfo } from '@njt/foundation/defs/comm';
import { GetOBDPGIListReq, GetOBDPGIListResp, OBDPGI } from 'src/defs/sap/obd';
import _ from 'lodash'

const pageTitle = 'OBD PGI(非顺丰叫料)';
useTitleHelper(pageTitle);

const $d = reactive({
  tableData: [] as OBDPGI[],
  selectedItems: [] as OBDPGI[]
});

let fpsReq: GetOBDPGIListReq = reactive({
  isSuccess: null,
  obdNumber: null,
  createDtBeg: null,
  createDtEnd: null,
  pageSize: 40,
  pageNo: 1,
  orderBy: null,
  isDescending: false
});

const columnsDefine: ColumnInfo[] = [
  {
    label: '操作',
    name: 'operation',
    field: 'operation',
    align: 'left',
    defVisible: true,
    defOrder: 1
  },
  {
    label: '序号',
    name: 'pgiId',
    field: 'pgiId',
    align: 'left',
    defVisible: true,
    sortable: true,
    defOrder: 2
  },
  {
    label: 'OBD Number',
    name: 'obdNumber',
    field: 'obdNumber',
    align: 'left',
    defVisible: true,
    defOrder: 3
  },
  {
    label: 'StatusCode1',
    name: 'statusCode1',
    field: 'statusCode1',
    align: 'left',
    defVisible: true,
    defOrder: 4
  },
  {
    label: 'StatusCode',
    name: 'statusCode2',
    field: 'statusCode2',
    align: 'left',
    defVisible: true,
    defOrder: 5
  },
  {
    label: '是否成功',
    name: 'isSuccess',
    field: 'isSuccess',
    align: 'left',
    defVisible: true,
    defOrder: 23
  },
  {
    label: '错误信息',
    name: 'backMsg',
    field: 'backMsg',
    align: 'left',
    defVisible: true,
    defOrder: 24,
    format: (val: string | null) => {
      return (val && val.length>130)?`${val.slice(0, 129)}...`:val
    }
  },
  {
    label: '创建人',
    name: 'createP',
    field: 'createP',
    align: 'left',
    defVisible: true,
    defOrder: 25,
    format: (val: string, row: any) => $f.fmt.userDisp(val, row.createPn)
  },
  {
    label: '创建时间',
    name: 'createDt',
    field: 'createDt',
    align: 'left',
    defVisible: true,
    defOrder: 26,
    format: (val: string) => $f.fmt.dtFmtFullNoSec(val)
  }
];

let listDataLoadingState = ref(false);
const loadListData = function(resetFpsReq?: number) {
  if (resetFpsReq) {
    if (resetFpsReq >= 1) {
      fpsReq.pageNo = 1;
    }
    if (resetFpsReq >= 2) {
      fpsReq.orderBy = '';
      fpsReq.isDescending = false;
    }
  }
  listDataLoadingState.value = true;
  $f.axios
    .get<GetOBDPGIListResp>('/mhlbc/sap/get-obd-pgi-list', { params: fpsReq })
    .then((resp) => {
      if (resp.data.createDtBeg)
        resp.data.createDtBeg = $f.fmt.dtFmtDateOnly(resp.data.createDtBeg);
      if (resp.data.createDtEnd)
        resp.data.createDtEnd = $f.fmt.dtFmtDateOnly(resp.data.createDtEnd);
      fpsAssist.handleGenericFpsResp(resp.data);
      $d.tableData = resp.data.obdPgis;
      fpsAssist.saveFpsReq(fpsReq);
    })
    .catch((err) => {
      $f.ui.showNotifyFailed($f.getAxiosErrMsg(err));
    })
    .finally(() => {
      listDataLoadingState.value = false;
    });
};

const {
  fpsInfo,
  fpsNav,
  fpsAssist,
  searchBarVisibility
} = useFpsHelper('/sap/obd-pgi',
  '/sap/obd-pgi/detail',
  null,
  null,
  fpsReq, columnsDefine,
  null,
  loadListData
);

const onSend = () => {
  void $f.ui.messageBox({
    caption: '操作确认',
    text: '确定要手动触发OBDPGI信息至SAP吗?',
    icon: 'Question',
    buttons: 'OkCancel',
    delayClose: true,
  }).then((btn) => {
    if (btn === 'ok') {
      $f.axios
        .post('/mhlbc/sap/hand-send-obd-pgi-info-sync-for-sap', _.map($d.selectedItems, 'pgiId'))
        .then(() => {
          $f.ui.closeMsgBox()
          loadListData(2)
        })
        .catch((err) => {
          void $f.ui.messageBox({
            caption: '发送失败',
            text: $f.getAxiosErrMsg(err),
            icon: $f.getAxiosErrIcon(err),
            buttons: 'Close',
          })
        })
    }
  })
}

const fx = $f;
</script>
